package com.stm.bi.jdbc;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author 阿俊
 * @date 2018/8/21
 */
@Configuration
@EnableTransactionManagement
public class MultiDataSourceConfig {

    private final static String DATASOURCE_PREFIX = "spring.datasource";


    public final static String DATASOURCE_ODS_PREFIX = DATASOURCE_PREFIX+"-ods";
    public final static String DATASOURCE_DMP_PREFIX = DATASOURCE_PREFIX+"-dmp";
    public final static String DATASOURCE_DATA_SYNC_PREFIX = DATASOURCE_PREFIX+"-datasync";
    public final static String DATASOURCE_REPORT_PREFIX = DATASOURCE_PREFIX+"-report";



    @Bean(name = DATASOURCE_ODS_PREFIX)
    @ConfigurationProperties(prefix= DATASOURCE_ODS_PREFIX)
    public DataSource odsDataSource() {
        DataSource dataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();
        return dataSource;
    }

    @Bean(name = DATASOURCE_DMP_PREFIX)
    @ConfigurationProperties(prefix= DATASOURCE_DMP_PREFIX)
    public DataSource dmpDataSource() {
        DataSource dataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();
        return dataSource;
    }

    @Bean(name = DATASOURCE_DATA_SYNC_PREFIX)
    @ConfigurationProperties(prefix= DATASOURCE_DATA_SYNC_PREFIX)
    public DataSource dataSyncDataSource() {
        DataSource dataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();
        return dataSource;
    }

    @Bean(name = DATASOURCE_REPORT_PREFIX)
    @ConfigurationProperties(prefix= DATASOURCE_REPORT_PREFIX)
    public DataSource reportDataSource() {
        DataSource dataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();
        return dataSource;
    }
}
